package de.lgohlke.serializer;

import org.msgpack.MessagePack;
import org.msgpack.MessagePackObject;

/**
 * <p>MessagePackSerializer class.</p>
 *
 * @author lars
 * @version $Id: $
 */
public class MessagePackSerializer<T> implements ISerializer<T>
{
  private final T instance;

  /**
   * <p>Constructor for MessagePackSerializer.</p>
   *
   * @param instance a T object.
   * @param <T> a T object.
   */
  public MessagePackSerializer(final T instance)
  {
    this.instance = instance;
  }

  /**
   * <p>serialize.</p>
   *
   * @param data a T object.
   * @return an array of byte.
   */
  public byte[] serialize(final T data)
  {
    return MessagePack.pack(data);
  }

  /**
   * <p>deserialize.</p>
   *
   * @param data an array of byte.
   * @return a T object.
   */
  public T deserialize(final byte[] data)
  {
    MessagePackObject dynamic = MessagePack.unpack(data);
    return dynamic.convert(instance);
  }
}
